package cn.edu.pet.repo;


import cn.edu.pet.db.BaseRepo;
import cn.edu.pet.db.ResultSetVisitor;
import cn.edu.pet.entity.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserRepo extends BaseRepo {
    private UserRepo() {

    }

    private static UserRepo instance = new UserRepo();

    public static UserRepo getInstance() {
        return instance;
    }

    public User selectByName(String name) throws SQLException {
        String sql = String.format("select * from user where username = %s", name);
        User user = new User();
        this.query(sql, resultSet -> {
            try {
                user.setId(resultSet.getInt(1));
                user.setUsername(resultSet.getString(2));
                user.setPassword(resultSet.getString(3));
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        });
        return user;
    }

    public boolean insertUser(User user){
        String sql = String.format("insert into user(username,password,tel,gender,address) value('%s','%s','%s','%s','%s')", user.getUsername(), user.getPassword(), user.getTel(), user.getGender(), user.getAddress());
        try {
            return this.execute(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public List<User> selectList(String name) {
        String sql;
        if (name != null && !"".equals(name)) {
            sql = String.format("select * from user where username = %s", name);
        } else {
            sql = "select * from user";
        }
        List<User> list = new ArrayList<>();
        this.query(sql, new ResultSetVisitor() {
            @Override
            public void visit(ResultSet resultSet) throws SQLException {
                User user = new User();
                user.setId(resultSet.getInt(1));
                user.setUsername(resultSet.getString(2));
                user.setPassword(resultSet.getString(3));
                user.setTel(resultSet.getString(4));
                user.setGender(resultSet.getString(5));
                user.setAddress(resultSet.getString(6));
                list.add(user);
            }
        });
        return list;
    }

    public boolean deleteUser(Integer id) {
        String sql = String.format("delete from user where id = %s", id);
        try {
            return this.execute(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public User selectById(Integer id) {
        String sql = String.format("select * from user where id = %s", id);
        User user = new User();
        this.query(sql, resultSet -> {
            try {
                user.setId(resultSet.getInt(1));
                user.setUsername(resultSet.getString(2));
                user.setPassword(resultSet.getString(3));
                user.setTel(resultSet.getString(4));
                user.setGender(resultSet.getString(5));
                user.setAddress(resultSet.getString(6));
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        });
        return user;
    }

    public boolean updateUser(User user) {
        String sql = String.format("update user set username = '%s',password = '%s',tel = '%s',gender = '%s',address = '%s' where id = %s", user.getUsername(), user.getPassword(), user.getTel(), user.getGender(), user.getAddress(), user.getId());
        try {
            return this.execute(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
